<?php
/**
 * 日志管理模块.
 * User: 1874
 * Date: 2019/5/8
 * Time: 15:05
 */

namespace app\adminplatform\controller;

use app\adminplatform\model\AuthGroup;
use app\adminplatform\model\Log as LogModel;
use think\facade\Request;
use think\facade\Session;
use think\facade\View;

class Log extends Initial {
    /*
     * 操作日志
     */
    public function logList(){
        $map = [];
        if(input("content")) $map[] = ["lo_content", "like", "%".input("content")."%"];
        if(input('start')) {
            $start = strtotime(input('start').'00:00:00');
            $end = strtotime(input('end').'23:59:59');
            $map[] = ['lo_time','between',"$start, $end"];
        }
        $info = LogModel::logList($map);
        //管理员权限
        $rules = ['Log/logExport', 'Log/logDelAll',];
        $check = AuthGroup::checkMethodArray(Session::get('userData'), $rules);

        View::assign('info',$info);
        View::assign('check',$check);
        return View::fetch('logList');
    }
    /*
     * 日志批量删除
     */
    public function logDelAll(){
        //判断请求类型
        if(!Request::isPost()){
            $data = ['info'=>'请求类型不是这个哦，亲','status'=>0];
        }else {
            //删除的分类id
            $ids = input('ids');
            //角色下方是否还有所属角色
            $info = LogModel::where("lo_id", "in", $ids)->delete();
            if($info){
                //写入日志
                LogModel::logAdd(Session::get('userData.ad_id'), '删除'.$info.'条日志');

                $data = ['info'=>'删除成功','status'=>1];
            }else{
                $data = ['info'=>'不好意思，删除失败了','status'=>0];
            }
        }
        return json($data);
    }
    /*
     * 日志导出
     */
    public function logExport(){
        $map = [];
        //日期区间
        if(input('start')) {
            $start = strtotime(input('start').'00:00:00');
            $end = strtotime(input('end').'23:59:59');
            $map[] = ['lo_time','between',"$start, $end"];
        }
        //查询
        $info = LogModel::logList($map, 1);
        //遍历赋值
        $info = arrayEach($info->toArray(), function ($item){
            $item['lo_time'] = date('Y-m-d H:i:s',$item['lo_time']);
            return $item;
        });
        $data = [
            '管理员','内容','IP地址','IP','时间'
        ];
        $files = [
            'ad_name','lo_content','lo_address','lo_ip','lo_time'
        ];
        $export = exportData($data,$info,$files);

        return json($export);
    }
}